/**   
 * @Title: MainActivity.java 
 * @Package cn.edu.cdtu.healthclient.activity 
 * @Description: TODO 
 * @author 
 * @date 
 */
package cn.edu.cdtu.healthclient.activity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;
import cn.edu.cdtu.healthclient.service.HealthService;
import cn.edu.cdtu.healthclient.service.HealthService.HealthBinder;
import cn.edu.cdtu.healthclient.task.ActivityTaskQueue;
import cn.edu.scu.healthclient.activity.R;

/** 
 * @ClassName: MainActivity 
 * @Description: 该类为主界面，是一个Fragment碎片。
 * @author 
 * @date 2016
 *  
 */
public class MainActivity extends FragmentActivity {
	
	/*
	 * 
	 */
	private ActivityTaskQueue taskQueue;
	public ActivityTaskQueue getTaskQueue(){
		return taskQueue;
	}
	/*
	 * 
	 */
	private HealthBinder binder;
	public HealthBinder getBinder(){
		return binder;
	}
	/*
	 * 
	 */
	private ServiceConnection serviceConnection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			binder = null;
			taskQueue.shutdownTaskQueue();
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			binder = (HealthBinder)service;
			if(!taskQueue.isRunning()){
				taskQueue.start();
			}
		}
	};
	/*
	 * 
	 */
	private final Intent serviceIntent = new Intent(HealthService.ACTION);
	//主界面中的其他几个子界面也由碎片组成。
	private Fragment[] fragments;  
	
	 
	private FragmentManager fragmentManager;  
	private FragmentTransaction fragmentTransaction;
	
	private LinearLayout yyll,zxll,csll,dall,dzll;
	/*
	 * (non-Javadoc)
	 * <p>Title: onCreate</p> 
	 * <p>Description: </p> 
	 * @param savedInstanceState 
	 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
	 */
	@Override
    protected void onCreate(Bundle savedInstanceState) 
    {
    	startService(serviceIntent);
    	taskQueue = new ActivityTaskQueue();

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //初始化其他几个界面。
        fragments = new Fragment[5];
        //该对象负责管理以下的几个界面。
        fragmentManager = getSupportFragmentManager();  
        //专家预约界面
        fragments[0] = fragmentManager.findFragmentById(R.id.ExpertAppoint);  
        //健康新闻界面
        fragments[1] = fragmentManager.findFragmentById(R.id.HealthNews);  
        //健康测试界面
        fragments[2] = fragmentManager.findFragmentById(R.id.HealthTest);
        //健康文档界面
        fragments[3] = fragmentManager.findFragmentById(R.id.HealthFile);
        //检查界面
        fragments[4] = fragmentManager.findFragmentById(R.id.Examine);
        fragmentTransaction = fragmentManager.beginTransaction()
        		.hide(fragments[0])
        		.hide(fragments[1])
        		.hide(fragments[2])
        		.hide(fragments[3])
        		.hide(fragments[4]);  
        //默认情况下，首先展示 健康新闻界面。
        fragmentTransaction.show(fragments[1]).commit();
        
        
        yyll = (LinearLayout) findViewById(R.id.yuyue_button);
        zxll = (LinearLayout) findViewById(R.id.zixun_button);
        csll = (LinearLayout) findViewById(R.id.ceshi_button);
        dall = (LinearLayout) findViewById(R.id.dangan_button);
        dzll = (LinearLayout) findViewById(R.id.daozhen_button);
        
        zxll.setSelected(true);
        
        yyll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				yyll.setSelected(true);
				zxll.setSelected(false);
				csll.setSelected(false);
				dall.setSelected(false);
				dzll.setSelected(false);
				
				fragmentTransaction = fragmentManager.beginTransaction()
						.hide(fragments[1])
		        		.hide(fragments[2])
		        		.hide(fragments[3])
		        		.hide(fragments[4]);
				fragmentTransaction.show(fragments[0]).commit();
			}
		});
        
        zxll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				yyll.setSelected(false);
				zxll.setSelected(true);
				csll.setSelected(false);
				dall.setSelected(false);
				dzll.setSelected(false);
				
				fragmentTransaction = fragmentManager.beginTransaction()
						.hide(fragments[0])
		        		.hide(fragments[2])
		        		.hide(fragments[3])
		        		.hide(fragments[4]);
				fragmentTransaction.show(fragments[1]).commit();
			}
		});
        
        csll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				yyll.setSelected(false);
				zxll.setSelected(false);
				csll.setSelected(true);
				dall.setSelected(false);
				dzll.setSelected(false);
				
				fragmentTransaction = fragmentManager.beginTransaction()
						.hide(fragments[0])
		        		.hide(fragments[1])
		        		.hide(fragments[3])
		        		.hide(fragments[4]);
				fragmentTransaction.show(fragments[2]).commit();
			}
		});
        
        dall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				yyll.setSelected(false);
				zxll.setSelected(false);
				csll.setSelected(false);
				dall.setSelected(true);
				dzll.setSelected(false);
				
				fragmentTransaction = fragmentManager.beginTransaction()
						.hide(fragments[1])
		        		.hide(fragments[2])
		        		.hide(fragments[0])
		        		.hide(fragments[4]);
				fragmentTransaction.show(fragments[3]).commit();
			}
		});
        
        dzll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				yyll.setSelected(false);
				zxll.setSelected(false);
				csll.setSelected(false);
				dall.setSelected(false);
				dzll.setSelected(true);
				
				fragmentTransaction = fragmentManager.beginTransaction()
						.hide(fragments[1])
		        		.hide(fragments[2])
		        		.hide(fragments[3])
		        		.hide(fragments[0]);
				fragmentTransaction.show(fragments[4]).commit();
			}
		});
        
    }
	
    /**
     * 
     * @Title: setFragmentIndicator 
     * @Description: 本方法负责使用户可以通过按钮选择浏览那个界面。
     * @return void
     * @throws 
     *
     */


	/* (non-Javadoc)
	 * <p>Title: onStart</p> 
	 * <p>Description: </p>  
	 * @see android.support.v4.app.FragmentActivity#onStart()
	 */
	@Override
	protected void onStart() {
		//当登录界面一启动就为登录模块绑定一个Service服务，该服务负责联系服务器上的数据库。
		bindService(serviceIntent, serviceConnection, Service.BIND_AUTO_CREATE);
		super.onStart();
	}

	/* (non-Javadoc)
	 * <p>Title: onStop</p> 
	 * <p>Description: </p>  
	 * @see android.support.v4.app.FragmentActivity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		//当登录Activity运行在后台时，解除对Service服务的绑定。
		unbindService(serviceConnection);
	}

	private long exitTime = 0;
	/* (non-Javadoc)
	 * <p>Title: onKeyDown</p> 
	 * <p>Description: 设置手机按键事件，当按下手机按键后提示用户。
	 * @param keyCode
	 * @param event
	 * @return 
	 * @see android.support.v4.app.FragmentActivity#onKeyDown(int, android.view.KeyEvent)
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){   
	        if((System.currentTimeMillis()-exitTime) > 2000){  
	            Toast.makeText(getApplicationContext(), "再按一次退出", 
	            		Toast.LENGTH_SHORT).show();                                
	            exitTime = System.currentTimeMillis();   
	        } else {
	            finish();
	            //System.exit(0);
	        }
	        return true;   
	    }
	    return super.onKeyDown(keyCode, event);
	}

	//设置菜单中的 “退出”标签。
	private final static int MENU_EXIT = 0x111;
	/* (non-Javadoc)
	 * <p>Title: onCreateOptionsMenu</p> 
	 * <p>Description: </p> 
	 * @param menu
	 * @return 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	//在主页面中创建菜单。
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		//为菜单添加退出选项
		menu.add(0, MENU_EXIT, 0, "退出");
		return super.onCreateOptionsMenu(menu);
	}

	/* (non-Javadoc)
	 * <p>Title: onOptionsItemSelected</p> 
	 * <p>Description: 为菜单实现功能，及退出APP。
	 * @param item
	 * @return 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case MENU_EXIT:
			//结束当前Activity类
			finish();
			//结束 位于系统后台的Service服务。
			stopService(serviceIntent);
			//结束整个APP
            System.exit(0);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}  
	
}
